package com.base.util;

import android.annotation.SuppressLint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.base.control.MActivitySupport;
import com.base.model.BackgroundResource;
import com.base.model.FindViewable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 用于在activity和fragment的setConview中自动设置background或imageview
 * Created by yjh on 15-3-13.
 */
public class BackgroundSetter {
    private static List<BackgroundResource> list = new ArrayList<BackgroundResource>();
//    private static WeakHashMap<Integer, Drawable> drawableCache = new WeakHashMap<>();

    public static void addBackground(Collection<BackgroundResource> collection) {
        list.addAll(collection);
    }

    public static void addBg(BackgroundResource br) {
        list.add(br);
    }

    @SuppressLint("NewApi")
    public static void checkAndSet(FindViewable contentView, int layoutid) {
        int sdk = android.os.Build.VERSION.SDK_INT;
        for (BackgroundResource br : list) {
            View view = contentView.findViewIdForSetBackground(br.getViewid());
            if(view != null) {
                try {
                    switch (br.getType()) {
                        case 0:
                            Drawable drawable = DisplayUtil.drawableTransfer(contentView.getContextForSetBackground(), br.getDrawable());
                            if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                                view.setBackgroundDrawable(drawable);
                            } else {
                                view.setBackground(drawable);
                            }
//                        drawableCache.put(br.getDrawable(), drawable);
                            break; //as background
                        case 1:
                            contentView.setImageView((ImageView) view, br.getDrawable(), MActivitySupport.ImageLoaderType.DEFAULT, false);
                            break; //set in imageView
                        case 2:
                            if(StringUtil.isEmpty(br.getImgId()))
                                contentView.setImageView((ImageView) view, br.getDrawable(), MActivitySupport.ImageLoaderType.DEFAULT, false);
                            else {
                                contentView.setImageView((ImageView) view, br.getDrawable(), MActivitySupport.ImageLoaderType.DEFAULT,
                                        false, br.getImgId(), -1, -1, br.getDrawable(), false, false, null);
                            }
                            break;
                    }
                }catch (OutOfMemoryError oom) {
//                    System.gc();
//                    System.runFinalization();
                    MLog.e(oom);
                }
            }
        }
    }

    public static void destroy(FindViewable contentView) {
        for (BackgroundResource br : list) {
            View view = contentView.findViewIdForSetBackground(br.getViewid());
            if(view != null) {
                switch (br.getType()) {
                    case 0:
                        BitmapDrawable bd = (BitmapDrawable)view.getBackground();
                        view.setBackgroundResource(0);//别忘了把背景设为null，避免onDraw刷新背景时候出现used a recycled bitmap错误
//                        bd.setCallback(null);
                        if(bd != null && bd.getBitmap() != null && !bd.getBitmap().isRecycled())
                            bd.getBitmap().recycle();
                        break;
//                    case 1:contentView.setImageView((ImageView)view, br.getDrawable(), MActivitySupport.ImageLoaderType.DEFAULT, false);break; //set in imageView
                }
            }
        }
    }
}
